<%--
  Created by IntelliJ IDEA.
  User: Cc
  Date: 2020/12/30
  Time: 16:51
  To change this template use File | Settings | File Templates.
--%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" +
            request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="<%=basePath%>static/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="<%=basePath%>static/css/font.css">
    <link rel="stylesheet" href="<%=basePath%>static/css/xadmin.css">
    <script type="text/javascript" src="<%=basePath%>static/js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="<%=basePath%>static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="<%=basePath%>static/js/xadmin.js"></script>
</head>

<body>
<div class="x-body">
    <form class="layui-form">
        <div class="layui-form-item">
            <label for="username" class="layui-form-label">
                <span class="x-red">*</span>登录名
            </label>
            <div class="layui-input-inline">
                <input onchange="checkUsername()" type="text" id="username" name="username"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="x-red">*</span>登入名
            </div>
        </div>

        <div class="layui-form-item">
            <label for="age" class="layui-form-label">
                年龄
            </label>
            <div class="layui-input-inline">
                <input type="number" id="age" name="age"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="hobby" class="layui-form-label">
                爱好
            </label>
            <div class="layui-input-inline">
                <input type="text" id="hobby" name="hobby"
                       autocomplete="off" class="layui-input">
            </div>
        </div>


        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label">
            </label>
            <button  class="layui-btn" id="L_repass" type="button" onclick="addUser()">
                添加
            </button>
        </div>
    </form>
</div>
<script>
<%--    检查用户是否存在--%>
function checkUsername(){
    // console.log($('#username').val())
    $.ajax({
        url:'../user/checkUsername',
        dataType: "json",
        type: "post",
        data:{
            "username":$('#username').val(),
        },
        success: function (res) {
            if(res.checkInfo=='查找成功'){
                $('#L_repass').show()
            }else{
                $('#L_repass').hide()
            }
            console.log(res);
        },error: function (res) {
            alert("服务器错误")
        }
    })
}

    function addUser() {
        $.ajax({
            url:'../user/addUser',
            dataType: 'json',
            type:'post',
            data:{
                "username":$('#username').val(),
                "password":$('#age').val(),
                "hobby":$('#hobby').val()
            },
            success: function (res) {
                let msg = res.addInfo;
                if(msg=='添加成功'){
                    layer.alert('修改成功',{icon:6},function(){
                        let index = parent.layer.getFrameIndex(window.name)
                        parent.layer.close(index);
                        parent.location.reload()

                    });
                }
                console.log(res)
            },
            error: function (res) {
                alert("服务器错误")
            }
        })
    }
</script>
</body>

</html>